/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package functions;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import model.Database;
import model.Doctor;

/**
 *
 * @author JArik
 */
public class Doctors {

    public static int addDoctor(String name, String surname, String login, String pass, int status) throws SQLException
    {
        Database db = new Database();
        int res=0;
        if(surname == null || surname.isEmpty())
            throw new SQLException("Укажите фамилию");
        if(login == null || login.isEmpty())
            throw new SQLException("Укажите учетную запись");
        
        ResultSet set = db.insertDBInfo("profiles", "name,surname,login,pass, status",
                "'"+name+"','"+surname+"','"+login+"','"+pass+"'"+","+status);
        if(set.next())
            res = set.getInt(1);
        db.closeStatement();
        db = null;
        return res;
    }

    public static void changeDoctor(int id, String name, String surname,
                                    String login, String pass, int status) throws SQLException {
        Database db = new Database();
        
        if(surname == null || surname.isEmpty())
            throw new SQLException("Укажите фамилию");
        if(login == null || login.isEmpty())
            throw new SQLException("Укажите учетную запись");

        db.updateDBInfo("profiles", 
                "name='"+name+"',"+
                "surname='"+surname+"',"+
                "login='"+login+"',"+
                "pass='"+pass+"',"+
                "status="+status,
                "id="+id);
        
        db.closeStatement();
        db = null;
    }

    public static void deleteDoctor(int id) throws SQLException{
        Database db = new Database();
        db.updateDBInfo("profiles", "deleted=1","id="+id);
        db.closeStatement();
        db = null;
    }

    public static int getIdFromNick(String nick) throws SQLException
    {
        Database db = new Database();
        int res=0;
        ResultSet set = db.getDBInfo("id", "profiles", "login='"+nick+"'");
        if(set.next())
            res = set.getInt(1);
        db.closeStatement();
        db = null;
        return res;
    }

    public static ArrayList<Doctor> getAll() throws SQLException
    {
        Database db = new Database();
        ArrayList<Doctor> doctors = new ArrayList<Doctor>();
        ResultSet set = db.getDBInfo("id, name, surname, login, pass, " +
                                     "status", "profiles",
                                     "deleted=0 ORDER BY surname");
        while(set.next())
        {
            Doctor doc = new Doctor(set.getInt("id"), set.getString("name"),
                                    set.getString("surname"));
            doc.setLoginName(set.getString("login"));
            doc.setStatus(set.getInt("status"));
            doc.setPass(set.getString("pass"));
            doc.setId(set.getInt("id"));

            doctors.add(doc);
        }
        db.closeStatement();
        db = null;
        return doctors;
    }
}
